﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using kad2SimSoft.Classes;
using System.Text.RegularExpressions;

namespace kad2SimSoft
{
    /// <summary>
    /// Interaktionslogik für ForecastControl.xaml
    /// </summary>
    public partial class ForecastControl : UserControl
    {
        private Boolean isFilled = true;
        private List<Endproduct> endproducts;
        private List<PurchasedPart> purchasedParts;
        private List<ManufacturedPart> manufacturedParts;
        private List<Forecast> forecasts;
        private List<Order> _orders = new List<Order>();
        private List<Waitinglist> _waitinglistStock = new List<Waitinglist>();
        private List<WaitinglistWorkplace> _waitinglistWorkplace = new List<WaitinglistWorkplace>();
        private List<Workplace> _workplaces = new List<Workplace>();
        private List<OrdersInWork> _ordersInWork = new List<OrdersInWork>();
        private MainWindow mainWindow;
        private List<AdditionalOrder> _additionalOrder;
        private Endproduct childBike;
        private Endproduct womanBike;
        private Endproduct manBike;
        public int fertig = 0;


        public List<Forecast> Forecasts
        {
            get { return forecasts; }
            set { forecasts = value; }
        }
        internal List<AdditionalOrder> AdditionalOrder
        {
            get { return _additionalOrder; }
            set { _additionalOrder = value; }
        }

        //private String test;

        public ForecastControl(List<PurchasedPart> purchasedParts, List<ManufacturedPart> manufacturedParts, 
                              List<Endproduct> endproducts, List<Order> orders, 
                              List<Waitinglist> waitinglistStock, List<WaitinglistWorkplace> waitinglistWorkplace,
                              List<Workplace> workplaces, List<OrdersInWork> ordersInWork)
        {
            InitializeComponent();
            this.endproducts = endproducts;
            this.purchasedParts = purchasedParts;
            this.manufacturedParts = manufacturedParts;
            this._orders = orders;
            this._waitinglistStock = waitinglistStock;
            this._waitinglistWorkplace = waitinglistWorkplace;
            this._workplaces = workplaces;
            this._ordersInWork = ordersInWork;
        }

         private void init() {

             int actPeriod = FindPageControl(this).ActPeriod;
             tbPeriodn.Text = actPeriod.ToString();
             tbPeriodn1.Text = (actPeriod + 1).ToString();
             tbPeriodn2.Text = (actPeriod + 2).ToString();
             tbPeriodn3.Text = (actPeriod + 3).ToString();
 
 
         }

         private void saveForecast() {

             isFilled = true;
             int childOrder = 0;
             int womanOrder = 0;
             int manOrder = 0;
             

             //test = "";

             //Validation der Felder
             foreach (Control control in InputForm.Children)
             {
                 if (control.GetType().ToString().Equals("System.Windows.Controls.TextBox"))
                 {

                     TextBox tb = (TextBox)control;
                     if (tb.Text.Equals(""))
                     {
                         //tb.BorderBrush = Brushes.Red;
                         isFilled = false;
                     }
                     else
                     {
                         //tb.BorderBrush = Brushes.Blue;
                     }
                 }

             }

             if (isFilled == false)
             {
                 MessageBox.Show("Bitte füllen Sie die rot markierten Felder aus!");
                 return;
             }
             else
             {

                 forecasts = new List<Forecast>();
                 _additionalOrder = new List<AdditionalOrder>();

                 childBike = (endproducts.Find(p => p.Id == 1));
                 womanBike = (endproducts.Find(p => p.Id == 2));
                 manBike = (endproducts.Find(p => p.Id == 3));


                 if (cbAdditionalOrder.IsChecked == true)
                 {

                     childOrder = Convert.ToInt32(tb_childActPeriod.Text) + Convert.ToInt32(tb_childAdditionalOrder.Text);
                     womanOrder = Convert.ToInt32(tb_womanActPeriod.Text) + Convert.ToInt32(tb_womanAdditionalOrder.Text);
                     manOrder = Convert.ToInt32(tb_manActPeriod.Text) + Convert.ToInt32(tb_manAdditionalOrder.Text);

                     //_additionalOrder.Add(new AdditionalOrder("1", Convert.ToInt32(tb_childAdditionalOrder.Text),
                     //    Convert.ToDouble(tb_childAddOrderPenalty.Text), Convert.ToDouble(tb_childAddOrderPrice.Text)));

                     //_additionalOrder.Add(new AdditionalOrder("2", Convert.ToInt32(tb_womanAdditionalOrder.Text),
                     //    Convert.ToDouble(tb_womanAddOrderPenalty.Text), Convert.ToDouble(tb_womanAddOrderPrice.Text)));

                     //_additionalOrder.Add(new AdditionalOrder("3", Convert.ToInt32(tb_manAdditionalOrder.Text),
                     //    Convert.ToDouble(tb_manAddOrderPenalty.Text), Convert.ToDouble(tb_manAddOrderPrice.Text)));

                     _additionalOrder.Add(new AdditionalOrder("1", tb_childAdditionalOrder.Text, tb_childAddOrderPenalty.Text, tb_childAddOrderPrice.Text));

                     _additionalOrder.Add(new AdditionalOrder("2", tb_womanAdditionalOrder.Text, tb_womanAddOrderPenalty.Text, tb_womanAddOrderPrice.Text));

                     _additionalOrder.Add(new AdditionalOrder("3", tb_manAdditionalOrder.Text, tb_manAddOrderPenalty.Text, tb_manAddOrderPrice.Text));

                 }
                 else
                 {

                     childOrder = Convert.ToInt32(tb_childActPeriod.Text);
                     womanOrder = Convert.ToInt32(tb_womanActPeriod.Text);
                     manOrder = Convert.ToInt32(tb_manActPeriod.Text);

                     //_additionalOrder.Add(new AdditionalOrder("1", 0,0.0,0.0));
                     //_additionalOrder.Add(new AdditionalOrder("2", 0, 0.0, 0.0));
                     //_additionalOrder.Add(new AdditionalOrder("3", 0, 0.0, 0.0));

                     _additionalOrder.Add(new AdditionalOrder("1", "0", "0", "0"));
                     _additionalOrder.Add(new AdditionalOrder("2", "0", "0", "0"));
                     _additionalOrder.Add(new AdditionalOrder("3", "0", "0", "0"));

                 }

                 try
                 {

                     forecasts.Add(new Forecast(childBike, childOrder, 0));
                     forecasts.Add(new Forecast(womanBike, womanOrder, 0));
                     forecasts.Add(new Forecast(manBike, manOrder, 0));

                     forecasts.Add(new Forecast(childBike, Convert.ToInt32(tb_childFirstForecast.Text), 1));
                     forecasts.Add(new Forecast(womanBike, Convert.ToInt32(tb_womanFirstForecast.Text), 1));
                     forecasts.Add(new Forecast(manBike, Convert.ToInt32(tb_manFirstForecast.Text), 1));

                     forecasts.Add(new Forecast(childBike, Convert.ToInt32(tb_childSecondForecast.Text), 2));
                     forecasts.Add(new Forecast(womanBike, Convert.ToInt32(tb_womanSecondForecast.Text), 2));
                     forecasts.Add(new Forecast(manBike, Convert.ToInt32(tb_manSecondForecast.Text), 2));

                     forecasts.Add(new Forecast(childBike, Convert.ToInt32(tb_childThirdForecast.Text), 3));
                     forecasts.Add(new Forecast(womanBike, Convert.ToInt32(tb_womanThirdForecast.Text), 3));
                     forecasts.Add(new Forecast(manBike, Convert.ToInt32(tb_manThirdForecast.Text), 3));



                     //foreach (Forecast f in forecasts) {

                     //    test = test + "Artikel ID: " + f.Product.Id.ToString() + " Periode: " + f.Period + " Menge: " + f.Count + "\n";
                     //}
                     FindPageControl(this).Forecasts = forecasts;
                     //FindPageControl(this).fillProductDescriptions();
                     //mainWindow = new MainWindow(endproducts, purchasedParts, manufacturedParts, forecasts, _orders, _waitinglistStock, _waitinglistWorkplace, _workplaces);
                     //mainWindow.Show();
                     //MessageBox.Show("Forecast-Daten erfolgreich gefüllt! \n Bsp: \n" + test );

                 }
                 catch
                 {
                     throw new Exception();
                 }
             }
         }

        public static MainWindow FindPageControl(DependencyObject child)
        {
            DependencyObject parent = VisualTreeHelper.GetParent(child);

            if (parent == null) return null;

            MainWindow page = parent as MainWindow;
            if (page != null)
            {
                return page;
            }
            else
            {
                return FindPageControl(parent);
            }
        }


        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            init();

            if (fertig == 1)
            {

                lblWarningTab1.Visibility = Visibility.Visible;


            }
            else
            {
                lblWarningTab1.Visibility = Visibility.Hidden;

            }

        }

        private void cbAdditionalOrder_Click_1(object sender, RoutedEventArgs e)
        {
            if (cbAdditionalOrder.IsChecked == true)
            {
                tb_childAdditionalOrder.IsEnabled = true;
                tb_womanAdditionalOrder.IsEnabled = true;
                tb_manAdditionalOrder.IsEnabled = true;

                tb_childAddOrderPenalty.IsEnabled = true;
                tb_childAddOrderPrice.IsEnabled = true;

                tb_womanAddOrderPenalty.IsEnabled = true;
                tb_womanAddOrderPrice.IsEnabled = true;

                tb_manAddOrderPenalty.IsEnabled = true;
                tb_manAddOrderPrice.IsEnabled = true;
            }
            else
            {
                tb_childAdditionalOrder.IsEnabled = false;
                tb_womanAdditionalOrder.IsEnabled = false;
                tb_manAdditionalOrder.IsEnabled = false;


                tb_childAddOrderPenalty.IsEnabled = false;
                tb_childAddOrderPrice.IsEnabled = false;

                tb_womanAddOrderPenalty.IsEnabled = false;
                tb_womanAddOrderPrice.IsEnabled = false;

                tb_manAddOrderPenalty.IsEnabled = false;
                tb_manAddOrderPrice.IsEnabled = false;
            }

            forecastChange();


        }

        private void btn_nextToProdPlaning_Click(object sender, RoutedEventArgs e)
        {
            saveForecast();
            fertig = 1;
            FindPageControl(this).setButtonsEnabledAfterForecast();
            FindPageControl(this).ChangeViewTo(MainWindow.VIEWTYPE.PRODUCTIONPLANNING);
        }

        private void btn_backToImportXML_Click(object sender, RoutedEventArgs e)
        {
            FindPageControl(this).setButtonsDisableStart();
            FindPageControl(this).ChangeViewTo(MainWindow.VIEWTYPE.XMLIMPORT);
        }

        private void tb_childAdditionalOrder_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_womanAdditionalOrder_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_manAdditionalOrder_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_childAddOrderPenalty_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_womanAddOrderPenalty_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_manAddOrderPenalty_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_childAddOrderPrice_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_womanAddOrderPrice_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_manAddOrderPrice_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }



        private void tb_childActPeriod_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_womanActPeriod_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_manActPeriod_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_childFirstForecast_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_womanFirstForecast_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_manFirstForecast_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_childSecondForecast_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_womanSecondForecast_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_manSecondForecast_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_childThirdForecast_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_womanThirdForecast_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private void tb_manThirdForecast_KeyUp(object sender, KeyEventArgs e)
        {
            validation(sender);
        }

        private static bool isValueNumeric(string text)
        {
            Regex regex = new Regex("[^0-9.]+"); //regex that matches disallowed text
            return !regex.IsMatch(text);
        }

        private void validation(object sender) {

            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            //if (Convert.ToInt32(tb.Text) > 1200) { tb.Text = "1200"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            saveForecast();
            forecastChange();
        }

        private void forecastChange() {

            FindPageControl(this).setButtonsDisabledAfterChangedForecast();
            FindPageControl(this).ProducionPlanningView = null;
            FindPageControl(this).ProductionOrderView = null;
            FindPageControl(this).CapacityPlanView = null;
            FindPageControl(this).PurchasingView = null;

            if (fertig == 1)
            { 
                FindPageControl(this).ProductionOrderView = null;
                FindPageControl(this).CapacityPlanView = null;
                FindPageControl(this).PurchasingView = null;
                FindPageControl(this).ProducionPlanningView = null;
            }
            fertig = 0;
        }
    }
}
